package com.epam.web_project.bank.command.customer_commands;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.customer_dao.CustomerDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Customer;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class FindByLoginCommand implements Command {

	//match the login entered and the customer in DB
	//used for password recovery
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String resPage;
		String login = request.getParameter("login");
		MySQLDAOFactory daoFactory = new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		CustomerDAO customerDAO = daoFactory.getCustomerDAO();
		Customer customer = customerDAO.findByLogin(login);
		AppPropertiesLoader propLoader=AppPropertiesLoader.getInstance();
		if (customer != null && !customer.isDeleted()) {
			HttpSession session = request.getSession(true);
			session.setAttribute("customerTemp", customer);
			resPage = propLoader.getProperty("secret_question_page");
		} else {
			request.setAttribute("message",
					propLoader.getProperty("NO_LOGIN_FOUND"));
			resPage = propLoader.getProperty("login_page");
		}
		return resPage;
	}
	
}
